package models;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

import javax.persistence.OneToMany;

import play.data.validation.Required;
import play.db.jpa.Model;

@Entity
public class Organization extends Model {

	@Required
	public String name;
	@Required
	public String type;
	@OneToMany(cascade = CascadeType.ALL)
	public List<Department> department;
	@OneToMany(cascade = CascadeType.ALL)
	public List<Relation> relation;
    public int views;
    public boolean tagable;
    @ManyToMany
    public List<User> toBeNotified;
	public Organization(String name, String type) {
		toBeNotified = new ArrayList<User>();

		this.name = name;
		this.type = type;
		department = new ArrayList<Department>();
		relation = new ArrayList<Relation>();
		toBeNotified = new ArrayList<User>();

	}

	public void addDepartment(Department dep) {
		department.add(dep);

	}

	public void addRelation(Relation rel) {
		relation.add(rel);

	}

	/**
	 * @author Khalid Osama
	 * @param user to be added to the list of followers.
	 * this method adds the new user to the list of followers.
	 */
	public void addFollowers(User user) {
			toBeNotified.add(user);
	}
	
	/**
	 * @author Khalid Osama
	 * @param user to check whether following or not.
	 * this method checks whether the user is following or not.
	 * @return true if following, false if not.
	 */
	public boolean isFollowing(User user){
		if (toBeNotified.contains(user)) {
			return true;
		} else {
			return false;
		}
	}

	public static List<Organization> searchByorganization(String search) {
		List<Organization> temp = find("name=?", search).fetch();
		return temp;
	}

}
